package com.itheima.service.impl;

import com.itheima.domain.Book;
import com.itheima.domain.BookSelective;
import com.itheima.domain.Record;
import com.itheima.mapper.BookMapper;
import com.itheima.mapper.RecordMapper;
import com.itheima.service.BookService;
import com.itheima.util.PageHelperUtil;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @Author: auuuu4
 * @Date: 2024/07/09/15:15
 * @Description:
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Autowired
    RecordMapper recordMapper;




    @Override
    public int borrowBook(Book book) {
        recordMapper.insert(new Record(null,book.getName(),book.getIsbn(),book.getBorrower(),book.getBorrowTime(),book.getReturnTime()));
        return bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public int returnBook(Integer id) {
        return bookMapper.updateByPrimaryKeySelective(new Book(id,null,null,null,null,null,null,null,"2","",null,new Date()));

    }

    @Override
    public int returnConfirm(Integer id) {
        return bookMapper.updateByPrimaryKeySelective(new Book(id,null,null,null,null,null,null,null,"0",null,null,null));

    }

    @Override
    public PageResult selectNewBooks(Integer pageNum,Integer pageSize) {
        List<Book> list = PageHelperUtil.selectList(pageNum,pageSize,bookMapper,"selectNewBooks");
        return new PageResult(bookMapper.selectBookNum(), list);
    }

    @Override
    public Book findById(int id) {
        return bookMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult selectBooks(Integer pageNum, Integer pageSize) {
        List<Book> list = PageHelperUtil.selectList(pageNum,pageSize,bookMapper,"selectBooks");
        return new PageResult(bookMapper.selectBookNum(),list);
    }

    @Override
    public PageResult selectBorrowedBooksByBorrower(String name) {
        List<Book> bookList = new ArrayList<>();
        bookMapper.selectBorrowedBooksByBorrower(name);
        return new PageResult(bookMapper.selectBorrowedBookNumByBorrower(name),bookList);
    }

    @Override
    public PageResult selectBorrowedBooks() {
        List<Book> bookList;
        bookList = bookMapper.selectBorrowedBooks();
        return new PageResult(bookMapper.selectAllBorrowedBookNum(),bookList);
    }
    private String likeStr(String str){
        return '%'+str+'%';
    }

    @Override
    public int updateBook(Book book) {
        return book == null ? 0 : bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public PageResult selectBorrowedBooksSelective(Integer pageNum, Integer pageSize, BookSelective selective) {
        if(selective == null)
            return null;
        String s = selective.getName();
        if (s != null)
            selective.setName(likeStr(s));
        s = selective.getPress();
        if (s != null)
            selective.setPress(likeStr(s));
        s = selective.getAuthor();
        if (s != null)
            selective.setAuthor(likeStr(s));

        List<Book> list = PageHelperUtil.selectList(pageNum,pageSize,bookMapper,"selectBorrowedBooksSelective",new Object[]{selective});
        return new PageResult(bookMapper.selectBorrowedBookNumSelective(selective),list);
    }

    @Override
    public int addBook(Book book) {
        if(book == null)
            return 0;
        book.setUploadtime(new Date());
        return bookMapper.insert(book);
    }
}
